$(function () {
    //事件绑定
    bindEvent();
    //加载应用秘钥
    loadAppKey();
    //获取应用品牌
    setAppBrand();
    //获取应用信息
    setAppBaseInfo();
});


//事件绑定
function bindEvent() {
    //应用Key改变事件
    $('#appKey').change(function(){
        //获取应用Key
        var appKey = $(this).children('option:selected').val();
        //加载方法
        loadMethod(appKey);
        //设置生成签名启用
        $('#createBindObject').linkbutton('enable');
    });

    //方法改变事件
    $('#method').change(function(){
        //获取应用方法
        var method = $(this).children('option:selected').val();
        //加载方法版本
        loadVersion(method);
        //设置生成签名启用
        $('#createBindObject').linkbutton('enable');
    });

    //应用类型改变事件
    $('#callType').change(function(){
        //设置生成签名启用
        $('#createBindObject').linkbutton('enable');
        //获取应用类型
        var callType = $(this).children('option:selected').val();
        if((callType == 'ANDROID') || (callType == 'IOS')){
            //重置[应用品牌]和[应用信息]
            $("#appBrand").val("");
            $("#appBaseInfo").val("");
        }else{
            //设置应用品牌
            setAppBrand();
            //设置应用信息
            setAppBaseInfo();
        }

    });

}

/**
 * 设置应用品牌
 */
function setAppBrand(){
    //获取应用类型
    var callType = $("#callType").children('option:selected').val();
    if((callType == 'WAP') || (callType == 'PC')){
        //设置[应用品牌]和[应用信息]
        $("#appBrand").val(getAppBrand());
    }
}

/**
 * 获取应用信息
 */
function getAppBaseInfo(){
    //设置应用信息
    var callType = $("#callType").children('option:selected').val();
    var obj;
    //电脑端
    if(callType == 'PC'){
        //deviceType 默认为：Computer, isMobileVisit   默认为：false
        obj = {"browserVer": _AgentInfo.browserVer, "deviceType": _AgentInfo.deviceType, isMobileVisit: _AgentInfo.isMobileVisit, "oSSystem": _AgentInfo.oSSystem, userAgent: _AgentInfo.userAgent  };
    }

    //手机端
    if(callType == 'WAP'){
        obj = {"browserVer": _AgentInfo.browserVer, "deviceType": "Mobile", isMobileVisit: true, "oSSystem": "Android", userAgent: _AgentInfo.userAgent  };
    }
    return obj;
}


/***
 * 设置应用信息
 */
function setAppBaseInfo(){
    //设置应用信息
    var callType = $("#callType").children('option:selected').val();
    if((callType == 'WAP') || (callType == 'PC')){
        //设置[应用信息]
        $("#appBaseInfo").val(JSON.stringify(getAppBaseInfo()));
    }
}




/**
 * 获取浏览器类型
 * @returns {string}
 */
function getAppBrand(){
        return  _AgentInfo.browserName;
}



/**
 * 加载应用Key
 */
function loadAppKey() {
    $.get(CONTEXT_PATH + "/request/loadAppKey.do", {}, function (data) {
        var appKeyList = $.parseJSON(data);
        var options = "";
        $.each(appKeyList, function (i, n) {
            options += "<option value='" + n.appKey + "'>" + n.appKey + "</option>";
        });
        $("#appKey").append(options);
        var appKey = $("#appKey").val();
        //加载方法
        loadMethod(appKey);
    });
}

/**
 * 加载方法
 * @param appKey 应用秘钥
 */
function loadMethod(appKey) {
    $.get(CONTEXT_PATH + "/request/loadAppMethod.do", {"appKey": appKey}, function (data) {
        var methodSet = $.parseJSON(data);
        var options = "";
        $.each(methodSet, function (i, n) {
            options += "<option value='" + n + "'>" + n + "</option>";
        });
        $("#method").html("");
        $("#method").append(options);

        var method = $("#method").val();
        //加载版本
        loadVersion(method);
    });
}

/**
 * 加载版本
 * @param method 应用方法
 */
function loadVersion(method) {
    $.get(CONTEXT_PATH + "/request/loadVersion.do", {"method": method}, function (data) {
        var versionSet = $.parseJSON(data);
        var options = "";
        $.each(versionSet, function (i, n) {
            options += "<option value='" + n + "'>" + n + "</option>";
        });
        $("#version").html("");
        $("#version").append(options);
    });
}


/**
 * 生成参数模型
 */
function createBindObject() {
    var method = $("#method").val();
    var version = $("#version").val();
    $.get(CONTEXT_PATH + "/request/createBindObject.do", {"method": method, "version": version}, function (data) {
        $("#bindObject").val(data.result);
        //提示框
        messager(data)
        //生成参数模型成功
        if(data.code == 'PC_200'){
            $("#createBindObject").linkbutton("disable");
        }else{
            $('#createBindObject').linkbutton('enable');
        }
    });
}

/**
 * 生成sign签名
 */
function createSign(){
    var client = $("#client").val();
    var callType = $("#callType").val();
    var callVersion = $("#callVersion").val();
    var appkey = $("#appKey").val();
    var format = $("#format").val();
    var method = $("#method").val();
    var version = $("#version").val();
    var appBrand = $("#appBrand").val();
    var appBaseInfo = $("#appBaseInfo").val();
    var bindObject = $("#bindObject").val();
    var sessionId = $("#sessionId").val();

    //应用版不能为空
    if("" == callVersion){
        $.messager.show({ title:'操作提示', msg:'应用版不能为空', timeout:3000, showType:"slide" });
        return;
    }
    if("" == bindObject){
        $.messager.show({ title:'操作提示', msg:'请先生成业务参数', timeout:3000, showType:"slide" });
        return;
    }

    $.post(CONTEXT_PATH + "/request/createSign.do", {"client": client, "callType": callType, "callVersion": callVersion, "appkey": appkey, "format": format, "method": method,"version": version, "appBrand": appBrand, "appBaseInfo": appBaseInfo, "bindObject": bindObject, "sessionId": sessionId}, function (data) {
        $("#sign").val(data.result);
        messager(data)
    });

}


/**
 * 测试服务
 */
function requestFormSumbit(isSign){
    //是否开启签名
    if('true' == isSign){
        var sign = $("#sign").val();
        if("" == sign){
            $.messager.show({ title:'操作提示', msg:'请先生成签名配置', timeout:3000, showType:"slide" });
            return;
        }
    }
  document.getElementById("requestForm").submit();
}
